package com.edu.policy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.policy.domin.PageDto;
import com.edu.policy.mapper.UmsAdminRoleRelationMapper;
import com.edu.policy.model.*;
import com.edu.policy.service.AppUmsRoleService;
import com.edu.policy.service.UmsRoleMenuRelationService;
import com.edu.policy.service.UmsRoleResourceRelationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@Service
public class AppUmsRoleServiceImpl extends UmsRoleServiceImpl implements AppUmsRoleService {

    private final UmsRoleMenuRelationService roleMenuRelationService;
    private final UmsRoleResourceRelationService roleResourceRelationService;
    private final UmsAdminRoleRelationMapper umsAdminRoleRelationMapper;

    @Override
    public List<UmsMenu> getMenuList(Long adminId) {
        return umsAdminRoleRelationMapper.getMenuList(adminId);
    }

    @Override
    public List<UmsMenu> listMenu(Long roleId) {
        return umsAdminRoleRelationMapper.getMenuListByRoleId(roleId);
    }

    @Override
    public List<UmsResource> listResource(Long roleId) {
        return umsAdminRoleRelationMapper.getResourceListByRoleId(roleId);
    }

    @Override
    public PageDto<UmsRole> list(String keyword, Integer pageSize, Integer pageNum) {
        PageDto<UmsRole> result = new PageDto<>();
        Page<UmsRole> page = Page.of(pageNum, pageSize);
        Page<UmsRole> tempResult = this
                .lambdaQuery()
                .like(keyword != null && !keyword.isEmpty(),UmsRole::getName, keyword)
                .page(page);
        result.setList(tempResult.getRecords());
        result.setPages(tempResult.getPages());
        result.setTotal(tempResult.getTotal());
        result.setCurrentPage(tempResult.getCurrent());
        return result;
    }

    @Override
    public int allocMenu(Long roleId, List<Long> menuIds) {
        //先删除原有关系
        QueryWrapper<UmsRoleMenuRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",roleId);
        roleMenuRelationService.remove(queryWrapper);
        //批量插入新关系
        List<UmsRoleMenuRelation> temp = new ArrayList<>(menuIds.size());
        for (Long menuId : menuIds) {
            UmsRoleMenuRelation relation = new UmsRoleMenuRelation();
            relation.setRoleId(roleId);
            relation.setMenuId(menuId);
            temp.add(relation);
        }
        roleMenuRelationService.saveBatch(temp);
        return menuIds.size();
    }

    @Override
    public int allocResource(Long roleId, List<Long> resourceIds) {
        //先删除原有关系
        QueryWrapper<UmsRoleResourceRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",roleId);
        roleResourceRelationService.remove(queryWrapper);
        //批量插入新关系
        List<UmsRoleResourceRelation> temp = new ArrayList<>(resourceIds.size());
        for (Long resourceId : resourceIds) {
            UmsRoleResourceRelation relation = new UmsRoleResourceRelation();
            relation.setRoleId(roleId);
            relation.setResourceId(resourceId);
            temp.add(relation);
        }
        roleResourceRelationService.saveBatch(temp);
        // TODO
//        adminCacheService.delResourceListByRole(roleId);
        return resourceIds.size();
    }
}
